// pages/business/business.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

    searchcon: '输入关键字搜索商家',
    goodlist: [],
    pullFlag:true,
    topSearch: false,
    domHeight: '',
    xrz: false,
    page:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getBusiness();
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        console.log(res.windowHeight);
        that.setData({
          domHeight: res.windowHeight - 50
        })
      },
    })
  },
  // 获取滚动条当前位置
  onPageScroll: function (e) {
    if (e.scrollTop > 100) {
      this.setData({
        floorstatus: true,
        topSearch: true
      });
    } else {
      this.setData({
        floorstatus: false,
        topSearch: false
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log(app.globalData.searchContent);
    if (app.globalData.searchContent == '') {
      this.searchcon = '输入关键字搜索商家';
    } else {
      this.searchcon = app.globalData.searchContent;
      this.setData({
        goodlist: [],
        page:1
      });
      this.getBusiness();
    };
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

    if(this.data.pullFlag==true){
      this.getBusiness();
    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getBusiness() {
    var key = '';
    if (app.globalData.searchContent == '输入关键字搜索商家') {
      key = ''
    } else {
      key = app.globalData.searchContent;
    }
    var data = {
      key: key,
      page:this.data.page
    };
    var that = this;
    app.ajaxPost('product/getBusinessList', data, function (res) {
      console.log(res.data);
      var newGoodList = [...that.data.goodlist, ...res.data]
      if(res.data.length<10){
        that.setData({
         pullFlag:false,
         goodlist: newGoodList
       })
      }else{
        that.setData({
          pullFlag: true,
          goodlist: newGoodList,
          page: that.data.page+1
        })
      }
   
    })
  },
  all() {
    app.globalData.searchContent='';
    var key = '';
    this.setData({
      page: 1,
      pullFlag: true,
      goodlist: []
    })
    var data = {
      key: key
    };
    var that = this;
    app.ajaxPost('product/getBusinessList', data, function (res) {
      console.log(res);
      that.setData({
        goodlist: res.data
      })
    })
  },
  //回到顶部
  goTop: function (e) {  // 一键回到顶部
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  },
  xrzClick() {
    console.log('12133')
    if (this.data.xrz == false) {
      this.setData({
        xrz: true
      })
    } else {
      this.setData({
        xrz: false
      })
    }
  }
})